﻿using System;
using System.Collections;
using System.Collections.Generic;
using CalcSharp.Core;
using CalcSharp.Core.Containers;
using CalcSharp.Core.Plugin;
using System.Text;

namespace plug_roman
{
    class num2roman: ICMDPlugin
    {
        private IPluginHost host;

        public Tresult Run(ArrayList parameters)
        {
            Tresult res = new Tresult();
            res.floatingpt = double.NaN;

            double number = Convert.ToDouble(parameters[0]);
            if (number < 0 || number > 3999) throw new CSException("Value must be in the range 0 - 3999.");
            if (number == 0) return res;

            int[] values = new int[] { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
            string[] numerals = new string[] { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };

            StringBuilder result = new StringBuilder();

            for (int i = 0; i < 13; i++)
            {
                while (number >= values[i])
                {
                    number -= values[i];
                    result.Append(numerals[i]);
                }
            }

            host.TextColor = ConsoleTextColor.TextOK;
            host.writeln(" Resutlt: " + result.ToString());
            host.TextColor = ConsoleTextColor.NormalText;

            return res;
        }

        public string Name
        {
            get { return "NUM2ROMAN"; }
        }

        public Dictionary<string, string> autocomplete
        {
            get { return null; }
        }

        public string Help
        {
            get { return Properties.Resources.num2roman; }
        }

        public IPluginHost Host
        {
            get
            {
                return this.host;
            }
            set
            {
                this.host = value;
                this.host.Register(this);
            }
        }
    }
}
